#define _CRT_SECURE_NO_WARNINGS 1

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> decorateRecord(TreeNode* root)
    {
        if (root == nullptr) {
            return _ans;
        }

        _st.resize(2);
        int direct = 1;
        _st[0].push(root);

        while (!_st[0].empty() || !_st[1].empty())
        {
            if (direct == 1)
            {
                while (!_st[0].empty())
                {
                    TreeNode* top = _st[0].top();
                    _st[0].pop();
                    _level.push_back(top->val);
                    if (top->left)  _st[1].push(top->left);
                    if (top->right) _st[1].push(top->right);
                }
            }
            else
            {
                while (!_st[1].empty())
                {
                    TreeNode* top = _st[1].top();
                    _st[1].pop();
                    _level.push_back(top->val);
                    if (top->right) _st[0].push(top->right);
                    if (top->left)  _st[0].push(top->left);
                }
            }

            _ans.emplace_back(_level);
            _level.clear();
            direct = 1 - direct;
        }

        return _ans;
    }

private:
    std::vector<int> _level;
    std::vector<std::vector<int>> _ans;
    std::vector<std::stack<TreeNode*>> _st;
};